require 'digest/sha1'

class User < ActiveRecord::Base

def self.authenticate(login,password)
  
  
u = find_by_login(login)
if u

#puts encrypt_password(u.salt, password)
u = nil if encrypt_password(u.salt, password) != u.hashed_password
end  #verifie si le login existe bien dans l'object u du model 
u
end

def self.encrypt_password (salt, password)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

private
def create_new_salt
   object_id.to_s + 12345
end  

validates_uniqueness_of :login, :case_sensitive => false #permet de verifier que le login exit dans la base.

def password=(value)
  @password = value
  self.salt = create_new_salt
  self.hashed_password = self.class.encrypt_password(salt, value)
end


def validate
  errors.add(:password, 'is missing') if hashed_password.blank?
end
  
end
